package com.beiben.project.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.project.mapper.ProjectTeamMapper;
import com.beiben.project.domain.ProjectTeam;
import com.beiben.project.service.IProjectTeamService;

/**
 * 项目组成员Service业务层处理
 * 
 * @author 熊国强
 * @date 2020-12-11
 */
@Service
public class ProjectTeamServiceImpl implements IProjectTeamService 
{
    @Autowired
    private ProjectTeamMapper projectTeamMapper;

    /**
     * 查询项目组成员
     * 
     * @param teamId 项目组成员ID
     * @return 项目组成员
     */
    @Override
    public ProjectTeam selectProjectTeamById(Long teamId)
    {
        return projectTeamMapper.selectProjectTeamById(teamId);
    }

    /**
     * 查询项目组成员列表
     * 
     * @param projectTeam 项目组成员
     * @return 项目组成员
     */
    @Override
    public List<ProjectTeam> selectProjectTeamList(ProjectTeam projectTeam)
    {
        return projectTeamMapper.selectProjectTeamList(projectTeam);
    }

    /**
     * 新增项目组成员
     * 
     * @param projectTeam 项目组成员
     * @return 结果
     */
    @Override
    public int insertProjectTeam(ProjectTeam projectTeam)
    {
        StringBuilder isNotSubmitFileUser = new StringBuilder("以下人员已在项目组中：");
        boolean submitFileFlag = false;
        int result = -1;
        projectTeam.setCreateTime(DateUtils.getNowDate());
        if(null!=projectTeam.getUserIds()){
            for(Long userId : projectTeam.getUserIds()){
                projectTeam.setUserId(userId);
                ProjectTeam pt = new ProjectTeam();
                pt.setUserId(projectTeam.getUserId());
                pt.setProjectId(projectTeam.getProjectId());
                List<ProjectTeam> list = projectTeamMapper.selectProjectTeamList(pt);
                if(list.size()>0){
                    for(ProjectTeam team:list){
                        submitFileFlag = true;
                        isNotSubmitFileUser.append(team.getNickName() + "  ");
                    }
                }
            }
        }
        if (submitFileFlag) {
            throw new BaseException(isNotSubmitFileUser.toString());
        }else{
            for(Long userId : projectTeam.getUserIds()) {
                projectTeam.setUserId(userId);
                result = projectTeamMapper.insertProjectTeam(projectTeam);
            }
        }
        return result;
    }

    /**
     * 修改项目组成员
     * 
     * @param projectTeam 项目组成员
     * @return 结果
     */
    @Override
    public int updateProjectTeam(ProjectTeam projectTeam)
    {
        projectTeam.setUpdateTime(DateUtils.getNowDate());
        return projectTeamMapper.updateProjectTeam(projectTeam);
    }

    /**
     * 批量删除项目组成员
     * 
     * @param teamIds 需要删除的项目组成员ID
     * @return 结果
     */
    @Override
    public int deleteProjectTeamByIds(Long[] teamIds)
    {
        return projectTeamMapper.deleteProjectTeamByIds(teamIds);
    }

    /**
     * 删除项目组成员信息
     * 
     * @param teamId 项目组成员ID
     * @return 结果
     */
    @Override
    public int deleteProjectTeamById(Long teamId)
    {
        return projectTeamMapper.deleteProjectTeamById(teamId);
    }
}
